package org.wendu.wdoa.dep.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wendu.wdoa.common.ex.BusinessException;
import org.wendu.wdoa.common.page.PageParam;
import org.wendu.wdoa.common.page.PageUtils;
import org.wendu.wdoa.dep.dao.DepartmentDao;
import org.wendu.wdoa.dep.dto.DepDto;
import org.wendu.wdoa.dep.model.Department;
import org.wendu.wdoa.dep.service.DepartmentService;


import java.util.Map;

@Service
@Transactional//说明是事务性的
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentDao departmentDao;
    @Override
    public  Map<String,Object> getDepList(PageParam pageParam) {
        return PageUtils.getPage(pageParam,()->departmentDao.findDepList(pageParam));
    }

    @Override
    public void deleteDepId(Integer d_id) {
        boolean exists = departmentDao.findExistsId(d_id);
        if (exists){
            throw new BusinessException("员工存在，删除失败");
        }
         departmentDao.deleteDepName(d_id);
    }

    @Override
    public void addOneDep(DepDto depDto) {
        Integer newId = departmentDao.findNewId();
        depDto.setD_id(newId);
        departmentDao.insertDep(depDto);
    }

    @Override
    public void updateDep(Department department) {
        departmentDao.updateDepData(department);
    }

    @Override
    public void DelDepMulti(Integer[] d_ids) {
        for (Integer d_id:d_ids) {
            boolean exists = departmentDao.findExistsId(d_id);
            if (exists){
                throw new BusinessException("员工存在，删除失败");
            }
            departmentDao.deleteDepName(d_id);
        }
    }

}
